package com.yswee.mallnavi.view.coupon.list;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.widget.adapterview.withmodel.MListView;
import com.yswee.framework.widget.layoutview.ILayoutView;
import com.yswee.framework.widget.loading.LoadingPullToRefreshListView;
import com.yswee.mallnavi.entity.info.CouponEntity;
import com.yswee.mallnavi.model.BaseListModel;
import com.yswee.mallnavi.model.info.CouponListModel;
import com.yswee.mallnavi.view.coupon.IListView;

public class ListView extends LoadingPullToRefreshListView<CouponEntity> implements IListView {
	
	private String mCategoryId;
	private CouponListModel mListModel;

	private IActionCallback mActionCallback;

	public void setActionCallback(IActionCallback c) {
		mActionCallback = c;
	}

	public void setParams(String categoryId) {
		mCategoryId = categoryId;
		createMode();
		mListModel.setParams(categoryId);
		refresh();
	}

	private IActionCallback mActionCallbackProxy = new IActionCallback() {
		@Override
		public void onInfoClicked(View v, CouponEntity coupon) {
			if (mActionCallback != null) {
				mActionCallback.onInfoClicked(v, coupon);
			}
		}
	};
	
	public ListView(Context context) {
		super(context);
	}
	
	public ListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected boolean hideContentOnAction() {
		return false;
	}

	@Override
	protected BaseListModel<CouponEntity> createMode() {
		if (mListModel == null) {
			ITaskContext context = mContext instanceof ITaskContext ?
					(ITaskContext) mContext : null;
			mListModel = new CouponListModel(context);
		}
		return mListModel;
	}

	@Override
	protected boolean isReadyLoadData(boolean isFirstPage) {
		return !StringUtils.isEmpty(mCategoryId);
	}

	@Override
	protected boolean enabledLoadNextPage() {
		return false;
	}
	
	@Override
	protected int getListViewType() {
		return MListView.LISTVIEW_TYPE_VIEW;
	}
	
	@Override
	protected ILayoutView<CouponEntity> getLayoutView(int position, CouponEntity t, int itemViewType) {
		ListItem griditem = new ListItem(mContext);
		griditem.setActionCallback(mActionCallbackProxy);
		return griditem;
	}
	
}